/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.xuexiang.xuidemo.provider;
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.UntilTNT;
import com.xuexiang.xuidemo.ResourceTable;

import java.util.List;


/**
 * @since 2021-03-26
 */
public class DialogSetterProvider extends BaseItemProvider {
    //ListContainer数据集合
    private List<UntilTNT> list=null;

    private Context context;
    public DialogSetterProvider(List<UntilTNT> list,Context context){
        this.list=list;
        this.context=context;
    }
    //list的长度
    @Override
    public int getCount() {
        return list==null ? 0:list.size();
    }
    //返回当前的item
    @Override
    public Object getItem(int i) {
        if (list != null && i >= 0 && i < list.size()){
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_dialog_item, null, false);
        } else {
            cpt = component;
        }
        UntilTNT untilTNT = list.get(i);
        Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_text);
        Image image= (Image) cpt.findComponentById(ResourceTable.Id_item_img);
        text.setText(untilTNT.getName());

        if (untilTNT.isDisplay()){   //默认是显示的
            image.setVisibility(Component.VISIBLE);
            image.setPixelMap(untilTNT.getPicture());
        }else {
            image.setVisibility(Component.HIDE);
        }

        return cpt;
    }
}

